package com.cskaoyan.controller;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName:CouponController
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/11 20:16
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * 显示优惠券列表
     *
     * @return
     */
    @RequiresPermissions("admin:coupon:list")
    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam,MarketCoupon marketCoupon) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketCoupon> couponList = couponService.list(baseParam,marketCoupon);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(couponList);

        CommonData data = CommonData.data(pageInfo);

        return BaseRespVo.ok(data);
    }

    /**
     * 新增优惠券
     *
     * @return
     */
    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketCoupon marketCoupon) {

        MarketCoupon marketCoupon1 = couponService.create(marketCoupon);

        if (marketCoupon1 == null) {
            return BaseRespVo.invalidData("插入失败");
        }
        return BaseRespVo.ok(marketCoupon1);
    }

    /**
     * 更新优惠券
     *
     * @return
     */
    @RequiresPermissions("admin:coupon:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon) {
        MarketCoupon update = couponService.update(marketCoupon);
        if (update == null) {
            return BaseRespVo.invalidData("插入失败");
        }
        return BaseRespVo.ok(update);
    }

    /**
     * 优惠券删除
     *
     * @param marketCoupon
     * @return
     */
    @RequiresPermissions("admin:coupon:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon) {
        int delete = couponService.delete(marketCoupon);
        if (delete == 200) {
            return BaseRespVo.ok();
        }
        return null;
    }


    /**
     * 优惠券详情：用户信息
     *
     * @param baseParam
     * @param couponId
     * @return
     */
    @RequiresPermissions("admin:coupon:listuser")
    @RequestMapping("listuser")
    public BaseRespVo listUser(BaseParam baseParam,Integer couponId,Integer userId,Short status) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketCouponUser> marketCouponUsersList = couponService.listUser(baseParam,couponId,userId,status);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(marketCouponUsersList);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 优惠券详情：优惠券信息
     *
     * @return
     */
    @RequiresPermissions("admin:coupon:read")
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        MarketCoupon read = couponService.read(id);
        return BaseRespVo.ok(read);
    }

}
